﻿<?php
	class CurrencyModel extends Model {
		public function __construct(){
			$this->open(Model::MYSQL);
			$this->calledClass(get_class($this));
		}
		public function queryJoinTable(){
			$query = "SELECT * FROM Currencies ";
			return $query;
		}
		/*with WHERE word*/
	public function DisplayAdmin($query,$page=1,$perpage=20,$search = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "$query WHERE" ;
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= implode(" $operation ",$tempSearchFields);
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function GetAllCurrency(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Currencies " ;
		$result = parent::query($query);
		$count = mysql_num_rows($result);
		return $count;
	}
		public function QueryOneRecord($where=array()){
			$this->open(Model::MYSQL);
				$query = "SELECT * FROM Currencies " ;
				$temp=array();
				foreach($where as $fieldName => $fieldValue ){
					$temp[] = $fieldName . " = '" . $fieldValue . "' " ;
				}
				$query .= " WHERE " . implode( " AND " , $temp ) . " ; " ;
				$result = parent::query($query);
				$count = mysql_num_rows($result);
			return $count;
		}
		/*Multi table with find as object*/
	public function GetFieldWithMultiTable($query, $where=array()){
		$this->open(Model::MYSQL);
		if(is_array($where)&&!empty($where)){
			$query = " $query " ;
			$temp=array();
			foreach($where as $fieldName => $fieldValue ){
				$temp[] = $fieldName . "'" . mysql_real_escape_string( $fieldValue ) . "' " ;
			}
			$query .= " WHERE " . implode( " AND " , $temp ) . " ; " ;
			$result = parent::query($query);
			if($result!==null){
				$rows = $result ;
				$result = array();
				while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
			}
			return $result===null?$result:(empty($result)?null:$result[0]);
		}
	}
		public function version($table="",$field="id",$where=array()){
		$this->open(Model::MYSQL);
			return parent::getFieldValue($table,$field,$where);
		}
		
		public function delete($where=array(),$deleteSet=false){
			$this->open(Model::MYSQL);
			return parent::delete($where,$deleteSet);
		}
		
		public function save($fields=array()){
			$this->open(Model::MYSQL);
			return parent::save($fields);
		}
		
		public function edit($fields=array(),$where=array()){
			$this->open(Model::MYSQL);
			return parent::update($fields,$where);
		}
		
		public function findObject($fields=array()){
			$this->open(Model::MYSQL);
			return parent::findAsObject($fields);
		}
		public function getCurrenciesAsArray(){
			$this->open(Model::MYSQL);
			$query = "SELECT * FROM Currencies ;" ;
			$result = parent::query($query);
			$rows=array();
			while($row = mysql_fetch_object($result)){
				$rows[$row->id]=$row;
			}
			return $rows;
		}
	}